package com.peanut.web.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.peanut.model.Resource;
import com.peanut.model.ResourceTreeNode;
import com.peanut.service.ResourceService;
import com.peanut.service.ResourceTreeMaker;
import com.peanut.web.controller.BaseController;

/**
 * 模块管理控制器
 * @author Johan.Chenjh
 *
 * 2011-3-11
 */
@Controller
@RequestMapping(value = "/admin/module")
public class ResourceController extends BaseController {

	private ResourceService resourceService;
	private ResourceTreeMaker moduleTreeMaker;
	
	@Autowired
	public void setModuleTreeMaker(ResourceTreeMaker maker) {
		this.moduleTreeMaker = maker;
	}
	
	@Autowired
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	
	@RequestMapping(value = "/manage")
	public ModelAndView modules(ModelAndView mav) {
		return mav;
	}
	
	@RequestMapping(value = "/all")
	@ResponseBody
	public List<ResourceTreeNode> allModules() throws Exception {
		List<Resource> moduleList = resourceService.queryAllModules(null);
		List<ResourceTreeNode> treeList = this.moduleTreeMaker.create(moduleList);
		// StringWriter sw = new StringWriter();
		// ObjectMapper mapper = new ObjectMapper();
		// mapper.writeValue(sw, moduleList);
		// log.debug(sw);
		return treeList;
	}
	
	@RequestMapping(value = "/mybatis")
	@ResponseBody
	public List<Resource> queryTopLevelModules() throws Exception {
		List<Resource> moduleList = resourceService.queryTopResource(null);
		//List<HashMap<String, String>> list = resourceService.queryUrlResourceWithRoleDao("");
		//List<ResourceTreeNode> treeList = this.moduleTreeMaker.create(moduleList);
		// StringWriter sw = new StringWriter();
		// ObjectMapper mapper = new ObjectMapper();
		// mapper.writeValue(sw, moduleList);
		// log.debug(sw);
		return moduleList;
	}
	
	@RequestMapping(value = "/load")
	@ResponseBody
	public List<Resource> loadTopModulesByUser() {
		UserDetails userDetails = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String account = userDetails.getUsername();
		List<Resource> moduleList = resourceService.queryTopResourceByAccount(account);
//		ComparableComparator comparer = new ComparableComparator();
//		Collections.sort(moduleList);
		//List<ResourceTreeNode> treeList = this.moduleTreeMaker.create(moduleList);
		return moduleList;
	}
	
	@RequestMapping(value = "/loadModuleTree/{parentId}")
	@ResponseBody
	public List<ResourceTreeNode> loadSubModulesByUser(@PathVariable String parentId) {
		UserDetails userDetails = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String account = userDetails.getUsername();
		Map param = new HashMap();
		param.put("account", account);
		param.put("parentId", parentId);
		List<ResourceTreeNode> moduleList = resourceService.createSubResourceTreeByAccount(param);
		return moduleList;
	}
	
	@RequestMapping(value = "/merge")
	@ResponseBody
	public String merge(Resource module) {
		String result = null;
		log.debug(module.getId());
		//module = moduleManager.save(module);
		return result;
	}
	
	@RequestMapping(value = "/remove")
	@ResponseBody
	public String remove(Integer id) {
		resourceService.removeNodeAndChildren(id);
		return null;
	}
}
